package com.linux.huhx.business.dao;

import com.linux.huhx.base.BaseController;
import com.linux.huhx.utils.DateFormatUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huhx on 2017-05-15.
 */
public class LoginDao{
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public Map<String, String> sayHello() {
        String username = "huhx";
        Map<String, String> map = sqlSessionTemplate.selectOne("login.queryUserForTest", username);
        System.out.println("hello world" + map.get("address") + map.get("school"));
        return map;
    }

    // 校验邮箱是否存在,true表示存在，false表示不存在
    public boolean checkEmailExist(String email) {
        Map<String, String> userMap = this.sqlSessionTemplate.selectOne("login.queryUserEmailExist", email);
        if (userMap == null || userMap.size() < 1) {
            return false;
        }
        return true;
    }

    // 根据email查询出用户的主键id
    public Long queryUserIdByEmail(String email) {
        Map<String, Long> userMap = this.sqlSessionTemplate.selectOne("login.queryUserEmailExist", email);
        if (userMap == null || userMap.size() < 1) {
            return null;
        }
        return userMap.get("UserId");
    }

    // 校验密码是否匹配
    public boolean checkPasswordMatch(Map<String, String> map) {
        Map<String, String> userMap = this.sqlSessionTemplate.selectOne("login.queryPasswordMatch", map);
        if (userMap == null || userMap.size() < 1) {
            return false;
        }
        return true;
    }

    // 记录并当天密码错误的次数
    public void updateFailLoginCount(Map<String, Object> map) {
        // 如果时间不是今天，那么清零
        Map<String, String> dateMap = sqlSessionTemplate.selectOne("login.queryUserLoginDate", map);
        if (dateMap == null || dateMap.size() < 1) {
            sqlSessionTemplate.update("login.updateFailLoginCount", map);
            return;
        }
        String dateStr =  dateMap.get("NowDay");
        String nowStr = DateFormatUtils.formatDate(new Date());
        // 如果数据库没有记载日期，或者不是今天，将失败次数改为1
        if (StringUtils.isEmpty(dateStr) || !dateStr.equals(nowStr)) {
            sqlSessionTemplate.update("login.updateFailLoginCountClear", map);
        } else {
            sqlSessionTemplate.update("login.updateFailLoginCount", map);
        }
    }

    // 更新本次登录的时间
    public void updateUserLoginTime(Map<String, Object> map) {
        this.sqlSessionTemplate.update("login.updateSuccessLoginInfo", map);
    }

    // 得到用户的失败次数
    public int queryFailLoginCount(Map<String, Object> map) {
        // 由于注册的原因，failMap里面一定有数据
        Map<String, Integer> failMap = sqlSessionTemplate.selectOne("login.queryFailLoginCount", map);
        if (failMap == null || failMap.size() < 1) {
            return 0;
        }
        return failMap.get("FailCount");
    }
}
